import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
      //所有的任务列表
      list:[],
      inputvalue:'aaa',
      nextid:5,
      allstar:'all'
    
  },
  mutations: {
    //不能写异步操作
    //commit触发motations
    //要用方法就可以直接methods里面定义，要用值（插值表达式）就在computed里面定义
        initlist(state,list){
            state.list=list
        },
        //为inputvalue赋值
        setinputvalue(state,val){
            state.inputvalue=val
        },
        additem(state){
            const obj={
            id:state.nextid,
            info:state.inputvalue.trim(),
            done:false,}
            state.list.push(obj)
            state.nextid++;
            state.inputvalue='';
        },
        delitem(state,id){
           const i= state.list.findIndex(x=>x.id===id)
           if(i!=-1) 
           {state.list.splice(i,1)}
        },
        changstatus(state,param){
            const i= state.list.findIndex(x=>x.id===param.id)
            if(i!=-1) 
           {state.list[i].done=param.status}
        },
        clendone(state){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
            state.list= state.list.filter(x=>x.done===false)
    
        },
        changaoliang(state,key){
            state.allstar=key
        }
  },
  actions: {
    getlist(context){
         axios.get('/list.json').then(({data})=>{
            context.commit('initlist',data)
         }) 
    }
  },
  modules: {
   

  },
  getters:{
      unDone(state){
       return   state.list.filter(x=> x.done===false).length
      },
      infolist(state){
        if(state.allstar==="all") {return state.list}
        if(state.allstar==="undone") {return  state.list.filter(x=> x.done===false)}
        if(state.allstar==="done") {return  state.list.filter(x=> x.done===true)}
        return state.list
      }
           
  }
})
